package com.pudding.chatmemory;

import dev.langchain4j.data.message.AiMessage;
import dev.langchain4j.data.message.UserMessage;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.openai.OpenAiChatModelName;

public class FirstExample {
    public static void main(String[] args) {
        ChatMemory chatMemory = MessageWindowChatMemory.builder()
                .maxMessages(10)  // 最多记录10条消息
// .chatMemoryStore()
                .build();

        ChatLanguageModel model = OpenAiChatModel.builder()
                .apiKey("demo")
                .modelName(OpenAiChatModelName.GPT_4_O_MINI)
                .build();

        chatMemory.add(UserMessage.from("你好，我是⼩明，你是我最好的兄弟，叫做阿强"));
        AiMessage answer = model.generate(chatMemory.messages()).content(); // 获取模型生成的答案
        System.out.println(answer.text());
        chatMemory.add(answer);

        chatMemory.add(UserMessage.from("⼩明最好的朋友是谁？"));
        AiMessage answer2 = model.generate(chatMemory.messages()).content();
        System.out.println(answer2.text());
        chatMemory.add(answer2);
    }
}
